
import lesson from '../../config/school/lesson'
import school from '../../config/school/school'
let  randomString =(len) => {
    　　len = len ||6;
    　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    　　var maxPos = $chars.length;
     　　var pwd = '';
     　　for (let i = 0; i < len; i++) {
     　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    　　}
   　　return pwd;
    } 
let showClassList = async (ctx,next) =>{
    let data = await ctx.db.querys(`select * from class`)
    let userList = await ctx.db.querys(`select userid,name,type from user`)
      data.forEach(item=>{
          userList.forEach(jtem=>{
                if(item.teacherid===jtem.userid){
                    item.teacherid = jtem.name
                }
                if(item.coachid===jtem.userid){
                    item.coachid = jtem.name
                }
          })
      })
      data.forEach(item =>{
          lesson.forEach(jtem =>{
            jtem.key = jtem.key+''
              if(item.courseid===jtem.key){
                item.courseid = jtem.lesson
            }
          })
      })
      let lecturer = userList.filter(item=>item.type ==='讲师')
      let instructor = userList.filter(item=>item.type ==='辅导员' )
    ctx.body = {code:1,msg:'数据请求成功',data:{data,lecturer,instructor}}
}
let getLists = async (ctx,next) => {
    //获取班级列表
    let arr = await ctx.db.querys(`select userid,name,type from user`);
    let lecturer = arr.filter(item=>item.type ==='讲师')
    let instructor = arr.filter(item=>item.type ==='辅导员' )
    ctx.body = {code:1,msg:'请求成功',data:{lecturer,instructor,lesson,school}}
}
let addClass = async (ctx,next) =>{
    //添加班级
    let {obj} = ctx.request.body
    let str = new Date().getTime()+''
    let id = str.substring(str.length-2)+randomString(6)
    obj.classid =id
    let add = await ctx.db.insert('class',obj)
    if(!add.affectedRows) return ctx.body = {code:0,msg:'添加失败，请稍后再试'}
    ctx.body = {code:1,msg:'添加成功'}
}
let startLesson = async (ctx,next) => {
     //开课接口
    let {obj} = ctx.request.body // obj中包含 startime开课时间,classid 班级名称 
    console.log(obj)
    if(obj.classnum==='50'){
        obj.type='大'
    }
    if(obj.classnum==='30'){
        obj.type='中'
    }
    if(obj.classnum==='10'){
        obj.type='小'
    }
    let add = await ctx.db.querys(`update class set startime='${obj.startime}',type='${obj.type}',coachid='${obj.coachid}',courseid='${obj.courseid}',classnum='${obj.classnum}' where classid='${obj.classid}' `)
        await ctx.db.querys(`update class set endtime='' where classid='${obj.classid}'`)
    if(!add.affectedRows) return ctx.body = {code:0,msg:'添加失败，请稍后再试'}
    ctx.body = {code:1,msg:'开课成功'}
}

let endlesson = async (ctx,next) => {
    //结课接口
    let {classid} = ctx.request.body
    let obj = {}
    let endtime = new Date().getTime()
        obj.endtime = endtime+''
    let arr = await ctx.db.querys(`select * from class where classid='${classid}' `)
    let updata = await ctx.db.querys(`update class set endtime='${endtime}' where classid='${classid}'`)
    if(!updata.affectedRows) return ctx.body={code:0,msg:'结课时间添加失败,请查看传输数据'}
    let obj1 = arr[0]
    obj.classid = classid;
    obj.schoolid = obj1.shoolid;
    obj.courseid = obj1.courseid;
    obj.userid = obj1.teacherid;
    obj.coachid = obj1.coachid;
    obj.classnum = obj1.classnum
    obj.startime = obj1.startime
    let str = new Date().getTime()+''
    let id = str.substring(str.length-2)+randomString(6)
    obj.recordid = id
    //需要加一个判断 判断讲师是否上传过月考录屏
    let brr = await ctx.db.querys(`select * from screen`) 
    // brr = brr.filter(item => item.userid === obj.userid &&item.type ==='月考' && item.classid === obj.classid  && item.createtime >obj.starttime&& item.createtime <obj.endtime )
    // if(!brr.length) return ctx.body={code:0,msg:'您还没有上传月考视频,请先上传视频再结课'}
    let add = await ctx.db.insert('classrecords',obj)
    if(!add.affectedRows) return ctx.body = {code:0,msg:'详情记录失败，请查看传输数据'}
    ctx.body = {code:1,msg:'结课成功'}
}

let getClassRecords = async (ctx,next) =>{
    //获取详情列表
    let {classid} = ctx.request.body
    let arr = await ctx.db.querys(`select * from classrecords`)
    if(!arr.length) return ctx.body={code:0,msg:'暂无数据',data:[]}
    arr =  arr.filter(item =>item.classid===classid)
    // 
    ctx.body = {code:1,msg:'查询完毕',data:arr} 
}
let getClassSituation = async (ctx,next) => {
    //获取班级相关信息 在跳转班级详情时 调用
    let {classid} = ctx.request.body
    let arr = await ctx.db.querys(`select * from class where classid='${classid}' `)
    let user = await ctx.db.querys(`select * from user`)
    arr.forEach(item =>{
        user.forEach(jtem=>{
            if(item.teacherid===jtem.userid){
                item.teacherid = jtem.name
            }
            if(item.coachid===jtem.userid){
                item.coachid = jtem.name
            }
        })
    })
    arr.forEach(item=>{
        lesson.forEach(jtem=>{
            if(item.courseid==jtem.key){
                item.courseid = jtem.lesson
            }
        })
    })
    let namelist = await ctx.db.querys(`select name,studentid from student where classid='${classid}'`)
    let brr = await ctx.db.querys(`select * from journal`)
    brr= brr.filter(item=>item.classid === classid)
    ctx.body = {code:1,msg:'获取成功',data:{arr,brr,lesson,namelist}}
}
let addClassSituation = async (ctx,next) => {
    //添加班级日志接口
    let {obj} = ctx.request.body
    let time = new Date(new Date().getTime()).setHours(0, 0, 0, 0)
    obj.savetime = time+'';
    let arr = await ctx.db.querys(`select startime,endtime from class where classid='${obj.classid}' `)
    let brr = await ctx.db.querys(`select * from journal`)
    brr = brr.filter(item=>item.studentid==obj.studentid&& item.savetime==obj.savetime)
    if(brr.length){
        ctx.body = {code:0,msg:'今日已添加该学生，不可重复添加'}
        return
    }
    let obj1= arr[0]
    if(!obj1.startime) {
         ctx.body ={code:0,msg:'开课之后才可以添加信息'}
         return 
    }
    let add = await ctx.db.insert('journal',obj)
    if(!add.affectedRows) return ctx.body = {code:0,msg:'请自己检查下数据传输的是否有误'}
    ctx.body = {code:1,msg:'添加完成'}
}
let classSearch = async (ctx,next) => {
    let {classname,shoolid,courseid}= ctx.request.body
    let arr =await ctx.db.querys(`select * from class`)
    let userList = await ctx.db.querys(`select userid,name,type from user`)
    if(classname){
        arr = arr.filter(item => item.classname.includes(classname))
    }
    if(shoolid){
        arr = arr.filter(item => item.shoolid === shoolid)
    }
    if(courseid){
        arr = arr.filter(item => item.courseid == courseid)
    }
    arr.forEach(item=>{
        userList.forEach(jtem=>{
              if(item.teacherid===jtem.userid){
                  item.teacherid = jtem.name
              }
              if(item.coachid===jtem.userid){
                  item.coachid = jtem.name
              }
        })
    })
    ctx.body = {code:1,msg:'搜索完成',data:arr}
}
export default {
    showClassList,getLists,addClass,startLesson,endlesson,getClassRecords,getClassSituation,addClassSituation,classSearch
}
